// Init App
var myApp = new Framework7({
    tapHold:true, //长按保持
    // Modal Setting
    modalTitle: '提示',
    modalButtonOk: '确定',
    modalButtonCancel: '取消',
    //modalPreloaderTitle: '正在载入...',
    //是否开启页面过渡动画
    //animatePages: true,
    animateNavBackIcon:true, //开启navbar动画
    //uniqueHistory: true, //路径唯一
    // notification settting
    notificationCloseOnClick: true, //设置为true之后，点击就可以关闭通知
    notificationTitle: '提示', //所有通知(notifications)的默认标题
    notificationCloseButtonText: '关闭', //关闭按钮文案
    //物理按键回退
    //pushState: true, //物理按键回退支持
    //swipeBackPage: true, //滑动返回上一页(IOS)
    sortable: false, //排序
    swipeout: false, //滑动删除
    materialPreloaderSvg: '<div class="icon-loading"></div>', //preLoader
    //template 7
    dynamicPageUrl: '{{name}}', //动态内容链接
    //template7Pages: true,
    //template7Data: {
    //
    //    'url:page/about.html': {
    //        title: 'TT语音',
    //        page_title: '关于TT',
    //        content: '测试'
    //    }
    //}
});

// Expose Internal DOM library
var $$ = Dom7;

// Add main view
var mainView = myApp.addView('.view-main', {
    domCache: true, //内联DOM
    dynamicNavbar: true,
});

// Show/hide preloader for remote ajax loaded pages
//$$(document).on('ajaxStart', function (e) {
//    //myApp.showIndicator();
//});
//$$(document).on('ajaxComplete', function () {
//    myApp.hideIndicator();
//});


myApp.isTransitionOn = false; //转场是否进行中

$$(document).on('pageInit', function (e) {
    myApp.isTransitionOn = true;
});

$$(document).on('pageAfterAnimation', function (e) {
    myApp.isTransitionOn = false;
});

// plugins
//init toast
//参数 message: 文本, html: html
myApp.showToast = function(message, html){
    //var toast = myApp.toast(message, '', {hold : 3000});
    //var html;
    //if(icon){
    //    html = '<div><img src="' + icon +'" /></div>';
    //}else {
    //    html = '<div></div>'
    //}

    var toast = myApp.toast(message, html);
    toast.show(true);
}

//init mainView.router
myApp.redictNewPage = function (pageName, navbar, toolbar, animation, history, query){
    var navbarInit = '';
    var navbarInitClass = '';
    var toolbarInit = '';
    var toolbarInitClass = '';

    if(navbar){
        navbarInit ='<div class="navbar">' +
                    '  <div class="navbar-inner">' +
                    '  </div>' +
                    '</div>' ;
        if (navbar == 'fixed') {
            navbarInitClass = ' navbar-fixed';
        }
        else {
            navbarInitClass = ' navbar-through';
        }

    }else if(_.isString(navbar)){
        navbarInit = '<div class=' + navbar + '></div>';
    }

    if(toolbar == true){
        toolbarInit = '<div class="toolbar"></div>';
        toolbarInitClass = ' toolbar-fixed';
    }else if(_.isString(toolbar)){
        toolbarInit = '<div class=' + toolbar + '></div>';
    }

    var navbarThrough = navbarInit +  '<div id=' + pageName + ' data-page=' + pageName + ' class="page' + toolbarInitClass + '">' + toolbarInit + '<div class="page-content"></div></div>';
    var navbarFixed = '<div id=' + pageName + ' data-page=' + pageName + ' class="page' + navbarInitClass + toolbarInitClass + '">' + navbarInit + toolbarInit + '<div class="page-content"></div></div>';

    var pageInit = (navbar == 'fixed') ? navbarFixed : navbarThrough;

    mainView.router.load({
        content :pageInit,
        animatePages: animation !=null ? animation : true,
        pushState: history !=null ? history : true,
        query: query != null ? query : null
    })
}

//init confirm alert
//myApp.confirmModal = function(description, btn_text, callback) {
//    myApp.modal({
//        title: '提示',
//        text: description,
//        buttons : [
//            {
//                text:
//            }
//        ]
//    })
//}

//webview 恢复后调用方法
myWebview.webviewCallbackWhenRecover = function(){

}
//网络变化时 webview被调用方法
myWebview.webviewCallbackWhenNetworkChange = function (){

}

//进度条通知
myWebview.onGameDownloadProgress = function(parameter){

}

//init game download button


//关注
myWebview.focusAreaRequest = function (){

}

myApp.updateButtonStatus = function(status){

}

//图片浏览
//参数说明: 序号 - index: 0, gallery: ['url']
myApp.photoBrowserPage = function(index, gallery){
    var template = '<div class="toolbar tabbar photo-browser-toolbar">' +
        '<div class="toolbar-inner">' +
        '<div class="center sliding">' +
        '<span class="photo-browser-current"></span>' +
        '<span class="photo-browser-of">{{ofText}}</span>' +
        '<span class="photo-browser-total"></span>' +
        '</div>'+
        '</div>' +
        '</div>';

    var myPhotoBrowserPopupDark = myApp.photoBrowser({
        photos : gallery,
        type: 'page',
        exposition: false,
        toolbar : true,
        maxZoom: 2,
        lazyLoading:true,
        lazyLoadingInPrevNext:true,
        ofText: '/',
        toolbarTemplate : template
    });
    myPhotoBrowserPopupDark.open(index);
}

//MPA外部分享页统计
var mpa_share_page = [
    { name: 'goddess_home_share', url: 'http://i.52tt.com/goddess/index' },
    { name: 'goddess_show_share', url: 'http://i.52tt.com/goddess/show/' },
    { name: 'goddess_sdk_share', url: 'http://app.52tt.com/project/active_goddess/index.html' },
    { name: 'official_mobile_home', url: 'http://m.52tt.com/' },
    { name: 'official_mobile_guild_recruit', url: 'http://i.52tt.com/recruit/game_list' },
    { name: 'official_mobile_zone', url: 'http://i.52tt.com/game/' },
    { name: 'cf_mobile_share', url: 'http://app.52tt.com/act/cf_game/index/mobile' },
]

//下载TT语音
//参数:  alert: true 先弹框确认再下载, 不带参数表示直接下载; title: 弹框标题; msg: 弹框文案
myApp.downloadTT = function(alert, title, msg){

    var urlParam = window.location.href;
    var extendParameter = $$.parseUrlQuery(urlParam);

    var android_package; //安卓渠道包
    var myapp_package; //应用宝渠道包
    //是否CF活动
    if(urlParam.toString().indexOf('http://app.52tt.com/act/cf_game/index/mobile') != -1){
        android_package = 'http://d.52tt.com/tt/CF2016_office/tt.apk';
        myapp_package = 'http://cdn.52tt.com/m/weixin_download/index.html';
    }else {
        android_package = 'http://d.52tt.com/tt/official/tt.apk';
        myapp_package = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.yiyou.ga';
    }



    function downloadApp(){

        //统计数据上报
        $$.each(mpa_share_page, function(index, item){
            if(urlParam.toString().indexOf(item.url) != -1){
                try{
                    _hmt.push(['_trackEvent', 'tt_multi_source', 'download', item.name]);
                }catch(e){}
            }else {
                try{
                    _hmt.push(['_trackEvent', 'tt_multi_source', 'download', 'marketing_channel']);
                }catch(e){}
            }
        })

        //执行下载逻辑
        var userAgent = navigator.userAgent.toLowerCase();
        //是否微信
        if(userAgent.match(/MicroMessenger/i) == "micromessenger") {
            //应用宝
            location.href = myapp_package;
        }else{
            //app
            if(myWebview.isInSdk()){
                if(window.tt_sdk.isAppInstalled("com.yiyou.ga")) {
                    window.tt_sdk.toast('正在打开TT语音', 0);
                    window.tt_sdk.openApp('com.yiyou.ga');
                }else{
                    window.tt_sdk.toast('开始下载TT语音', 0);
                    location.href = android_package;
                }
            }else if(myApp.device.ios){
                location.href = 'https://appsto.re/cn/15Rv8.i'; //iOS
            }else if(myApp.device.android){
                location.href = android_package;
            }
            else{
                location.href = 'http://m.52tt.com';
            }
        }
    }

    if(alert){
        var modalTitle = title ? title : '下载TT';
        var modalMsg = msg ? msg : '享受最强团战语音神器,千万福利礼包派不停';

        myApp.modal({
            title: modalTitle,
            text: modalMsg,
            buttons:[
                {
                    text: '暂不下载',
                    color: 'gray'
                },{
                    text: '立即下载',
                    color: 'green',
                    onClick: function(){
                        downloadApp ();
                    }
                }
            ],
        })
    }else {
        downloadApp ();
    }

}

